package search.impl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.Callable;

public class GetAllHtmlCaller extends GetAll implements Callable<ArrayList<String>>
{
	private String url;
	GetAllHtmlCaller(String url)
	{
		this.url = url;
	}

	@Override
	public ArrayList<String> call() throws Exception
	{
		ArrayList<String> returner = new ArrayList<String>();
		try
		{
			Document departmentDoc = Jsoup.parse(getAllHtml(url));
			Elements elementsByClass = departmentDoc.getElementsByClass("department-list");
			for (Element overviewEntry : elementsByClass)
			{
				for (Element overview : overviewEntry.getElementsByTag("a"))
				{
					String href = overview.attr("href");
					if(href.startsWith("https://"))
					{
						returner.add(getAllHtml(href));
					}
					else
					{
						returner.add(getAllHtml("https://www.liverpool.ac.uk" + href));
					}
				}
			}
		}
		catch (FileNotFoundException ignored)
		{
			ignored.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return returner;
	}
}
